<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="CSS/Tree.css"/>
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>
<script type="text/javascript" src="lib/datatables/js/jquery.dataTables.js"></script>

<link rel="stylesheet" type="text/css" href="${ctx}/css/tipswindown.css"/>
<script type="text/javascript" src="${ctx}/js/tipswindown.js"></script>

<h1 class="page-title">角色设置 - 添加</h1>

<div class="container_12 clearfix leading">

    <div class="side grid_12 leading">
        <form class="form has-validation" name="user_form" id="user_form" method="post">

            <div class="clear"></div>
            <div class="clearfix">

                <label for="select_name" class="form-label">选择职务 <em>*</em></label>

                <div class="form-input"><span id="select_name">${name}</span>
                    <select onchange="onChangeDuty(this)" id="dutySel">
                        <option value="-1">请选择</option>
                        <s:if test="#request.dutys != null and #request.dutys.size() > 0">
                        <s:iterator value="#request.dutys" var="d">
                        <option value="${d.id}" o_name="${d.dutyName}">${d.dutyName} 【${d.organization.path}】</option>
                        </s:iterator>
                        </s:if>
                    </select>
                </div>
            </div>

            <div class="clearfix">
                <label for="resource_list" class="form-label">权限设置</label>
                <div class="form-input" id="resource_list">
                    <table style="margin: 3px;">
                        <tr>
                            <td>
                                <input type="checkbox" value=""/><label style="margin-right: 10px;">浏览组织架构</label>
                            </td>
                        </tr>

                    </table>
                </div>
            </div>

            <div class="form-action clearfix">
                <button class="button" type="button" onclick="checkForm()">添加</button>
                <button class="button" type="button" onclick="onReset()">重置</button>
                <a class="button" href="#authority/role_main.xhtml"><span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
            </div>

        </form>
    </div>
</div>

<div class="clear"></div>


<script type="text/javascript" src="js/Tree.js"></script>
<script type="text/javascript">
    function onShowDiv(){
        //$("#config-popover").css("width", 900);
        tipsWindown("选择员工","url:post?${ctx}/user/select.xhtml?selection_type=multi_selection&organizationId=${organizationId}","600","350","true","","true","text");
    }

    //选择用户回调
    function onSelectUser(uid, uname){
        if(uid == "array"){
            var array = uname;//保存了id:name列表
            if(array != null && array.length > 0){
                var tempStr = "";
                for(var i=0; i<array.length; i++){
                    var user = array[i];
                    var offset = user.indexOf(":")
                    var id = user.substr(0, offset);
                    var name = user.substr(offset+1, user.length);
                    if(i == 0){
                        tempStr += '<a class="button sihai_user" href="javascript:void(0)" style="margin: 3px 5px;margin-left: 0px;" id="user_'+id+'">'+name+'</a>';
                    }else{
                        tempStr += '<a class="button sihai_user" href="javascript:void(0)" style="margin: 3px 5px;" id="user_'+id+'">'+name+'</a>';
                    }
                }
                $("#user_list").html(tempStr);
            }
        }
    }

    function onChangeDuty(obj){
        var v = $(obj).val();
        if(v == '-1'){
            $("#r_name").val("");
        }else{
            $("#r_name").val($("#dutySel").find("option:selected").attr("o_name"));
        }
    }

    $(function(){
        findResources("${ctx}/authority/resource-list.xhtml");
    });

    function findResources(url){
        $.ajax(url, {
            type:"POST",
            data:$("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                $("#resource_list").html(a);
            },
            complete:function(b) {
            }
        });
    }
    function loadListContent(url){
        findResources(url);
    }

    function onReset(){
        $("#dutySel").val("-1");
        $("#dutySel").prev("span").html("请选择");
        $("#r_name").val("");
        $("#user_list").html("");
        $("input:checkbox[name='resource_checkbox'][checked]").attr("checked", false);
    }

    function checkForm(){
        if($("#dutySel").val() == "-1"){
            alert("请选择职务");
            return;
        }
        if(checkNull($("#r_name").val())){
            alert("请输入角色名");
            return;
        }
        var userIds = new Array(); //用户ID列表
        $("a.sihai_user").each(function(){
            var temp = this.id;
            var id = temp.substr("user_".length, temp.length);
            userIds.push(id);
        });
        var resourceIds = new Array(); //权限ID列表
        $("input:checkbox[name='resource_checkbox'][checked]").each(function(){
            var temp = this.id;
            var id = temp.substr("resource_".length, temp.length);
            resourceIds.push(id);
        });
        var data = {
            "role.name":$("#r_name").val(),
            "role.duty.id":$("#dutySel").val(),
            "user_ids":userIds.join(","),
            "resource_ids":resourceIds.join(",")
        }
        var url = "${ctx}/authority/role_save.xhtml";
        $.ajax(url, {
            type:"POST",
            data:data,
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url);
            },
            complete:function(b) {
            }
        });
    }
</script>
    